﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Net.Http;

namespace WXTao
{
    class PDD
    {
        private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public PDD(){
        }
        
        /// 拼多多签名
        /// 
        /// 
        public static string GetSign(Dictionary<string, string> param)
        {
            //拼多多 签名认证
            //*签名算法
            //POP目前支持的签名算法为：MD5(sign_method=md5)，签名大体过程如下：
            //1-所有参数进行按照首字母先后顺序排列
            //2-把排序后的结果按照参数名+参数值的方式拼接
            //3-拼装好的字符串首尾拼接client_secret进行md5加密后转大写，secret的值是拼多多开放平台后台分配的client_secret
            string sclient_secret = string.Empty;
            string jstring = string.Empty;
            Dictionary<string, string> list = param.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
            foreach (KeyValuePair<string, string> kv in list)
            {
                if (kv.Key != "client_secret")
                {
                    jstring += kv.Key + kv.Value;
                }
                else
                {
                    sclient_secret = kv.Value;
                }

            }
            jstring = sclient_secret + jstring + sclient_secret;
            string md5jstring = MD5Helper.Md5(jstring).ToUpper();
            return md5jstring;
        }

        private string pddApi= "https://gw-api.pinduoduo.com/api/router";
        private string pddPID = "13370177_158534846";

        private string getParam(string method_name, Dictionary<string, string> p)
        {
            long timer = (long)(DateTime.Now - timeStampStartTime).TotalSeconds;
            Dictionary<string, string> pub_param = new Dictionary<string, string>();
            pub_param.Add("type", method_name);
            pub_param.Add("client_id", "c4051a9aef224a89b4b6f80ef4b051e4");
            pub_param.Add("client_secret", "77db832dd38a6b222bcc096d5cb3f8f6dfa58bb1");
            pub_param.Add("timestamp", timer + "");
            
            if(p != null)
            {
                foreach (KeyValuePair<string, string> kv in p)
                {
                    pub_param.Add(kv.Key, kv.Value);
                }
            }
            
            string sign = GetSign(pub_param);
            pub_param.Add("sign", sign);
            pub_param.Remove("client_secret");


            string param = "";
            foreach (KeyValuePair<string, string> kv in pub_param)
            {
                param += "&" + kv.Key + "=" + kv.Value;
            }

            param = param.Substring(1, param.Length - 1);
            return param;
        }

        public string getGoodsList2()
        {
            Dictionary<string, string> p = new Dictionary<string, string>();
            p.Add("channel_type", "1");
            p.Add("limit", "10");
            string param = getParam("pdd.ddk.goods.recommend.get", p);
            try
            {
                using (var client = new HttpClient())
                {
                    var responseString = client.GetStringAsync(pddApi + "?" + param);
                    return responseString.Result;
                }
            }
            catch (Exception e)
            {
                return e.Message.ToString();
            }
        }

        public string getGoodsList()
        {
            string param = getParam("pdd.ddk.top.goods.list.query",null);
            try
            {
                using (var client = new HttpClient())
                {
                    var responseString = client.GetStringAsync(pddApi+"?"+ param);
                    return responseString.Result;
                }
            }
            catch (Exception e)
            {
                return e.Message.ToString();
            }
            
        }

        public string getCpsUrl(long goodsid)
        {

            string goodsID = "[" + goodsid + "]";
            Dictionary<string, string> p = new Dictionary<string, string>();
            p.Add("p_id", pddPID);
            p.Add("goods_id_list", goodsID);
            string param = getParam("pdd.ddk.goods.promotion.url.generate",p);
            
            try
            {
                using (var client = new HttpClient())
                {
                    var responseString = client.GetStringAsync(pddApi + "?" + param);
                    return responseString.Result;
                }
            }
            catch (Exception e)
            {
                return e.Message.ToString();
            }
        }

    }
}
